//
//  DMBottomView.m
//  TabbarTest
//
//  Created by guosm on 2021/4/20.
//

#import "DMBottomView.h"
#import "LPButton.h"
@implementation DMBottomView{
    UIButton* _selectBtn;
}

- (void)setItems:(NSArray<NSDictionary *> *)items{
    if (_items == items) {
        return;
    }
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSInteger count = items.count;
    CGFloat w = self.bounds.size.width / 6.0;
    CGFloat h = self.bounds.size.height;

    for (NSInteger i = 0 ; i<count; i++) {
        NSDictionary *item = items[i];
        LPButton *btn = [[LPButton alloc] initWithFrame:CGRectMake(w*i, 0, w, h)];
        btn.tag = i;
        btn.style = LPButtonStyleTop;
        [btn setTitle:item[@"name"] forState:UIControlStateNormal];
        [btn setTitle:item[@"name"] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn addTarget:self action:@selector(clickItemBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:_selectTitleColor forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:item[@"selectedIcon"]] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:item[@"defaultIcon"]] forState:UIControlStateNormal];
        [self addSubview:btn];
    }
}
- (void)clickItemBtn:(UIButton*)sender{
    if (_selectBtn==sender) {
        return;
    }
    _selectBtn.selected = NO;
    _selectBtn = sender;
    _selectBtn.selected = YES;
}
@end
